package com.sls.mall.order.listener;

import com.rabbitmq.client.Channel;
import com.sls.mall.order.entity.OmsOrderEntity;
import com.sls.mall.order.service.OmsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 订单关闭消息监听
 */
@Slf4j
@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {

    @Autowired
    OmsOrderService orderService;

    /**
     * 监听订单过期消息
     * @param entity
     */
    @RabbitHandler
    public void listener(OmsOrderEntity entity, Channel channel, Message message) throws IOException {
        System.out.println("收到过期的订单信息：准备关单"+entity.getOrderSn());
        try{
            orderService.closeOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e){
            log.error("订单过期消息处理异常：", e);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
